<template>
    <Layout>
        <div>
            <Card dis-hover>
                <h3>
                    {{ taskInfo.taskName }}
                </h3>
                <ButtonGroup slot="extra" v-if="!taskInfo.viewFlag">

                    <Button @click="submitAndUpdate('showdata')" type="warning" style="background: #EF5350;"
                            :loading="buttonloading1" v-if="taskInfo.procinstState === '2'">
                        <Icon type="ios-bookmark"/>
                        审批
                    </Button>
                    <Button @click="submit('showdata')" type="warning" style="background: #EF5350;"
                            :loading="buttonloading1"
                            v-else>
                        <Icon type="ios-bookmark"/>
                        审批
                    </Button>

                    <Button @click="returnTo">
                        返回
                        <Icon type="ios-arrow-forward"/>
                    </Button>
                </ButtonGroup>
                <ButtonGroup slot="extra" v-else>
                    <Button @click="returnTo">
                        返回
                        <Icon type="ios-arrow-forward"/>
                    </Button>
                </ButtonGroup>

            </Card>
            <div>
                <Card dis-hover style="margin-top: 10px">
                    <div :style="{ overflowY: 'auto'}" v-if="taskInfo.procinstState === '2'">
                        <Col span="22">
                            <Form :model="showdata" ref="showdata" :rules="ruleCustom1" :label-width="160">
                                <Row type="flex" justify="space-around" class="code-row-bg">
                                    <!--<Col span="11">-->
                                    <!--<FormItem label="事故名称：" >-->
                                    <!--<Input v-model="showdata.sartName" disabled placeholder="请输入事故名称"/>-->
                                    <!--</FormItem>-->
                                    <!--</Col>-->
                                    <Col span="7">
                                        <FormItem label="事故类别：" prop="accTypeId">
                                            <Select v-model="showdata.accTypeId">
                                                <Option v-for="item in typeList" :value="item.codingCodeId"
                                                        :key="item.codingCodeId">
                                                    {{ item.codingName }}
                                                </Option>
                                            </Select>
                                        </FormItem>
                                    </Col>
                                    <Col span="7">
                                        <FormItem label="事故时间：" prop="position">
                                            <DatePicker type="date" v-model="showdata.sartDate" :clearable="false"
                                                        placeholder="请选择事故时间"
                                                        style="width: 100%"></DatePicker>
                                        </FormItem>
                                    </Col>
                                    <Col span="7">
                                        <FormItem label="填报人电话：" prop="phone">
                                            <Input type='text' v-model='showdata.phone'></Input>
                                        </FormItem>
                                    </Col>
                                </Row>
                                <Row type="flex" justify="space-around" class="code-row-bg">
                                    <Col span="7">
                                        <FormItem label="直接经济损失(万元)：" prop="loseMoney">
                                            <InputNumber type='text' v-model='showdata.loseMoney' disabled
                                                         style="width: 100%"></InputNumber>
                                        </FormItem>
                                    </Col>
                                    <Col span="7">
                                        <FormItem label="歇工天数：">
                                            <InputNumber :disabled="daysWork" placeholder="请填写歇工天数" min="0" type='text'
                                                         v-model='showdata.daysWork' disabled
                                                         style="width: 100%"></InputNumber>
                                        </FormItem>
                                    </Col>
                                    <Col span="7">
                                        <FormItem label="填报人：" prop="userName">
                                            {{ showdata.userName }}
                                        </FormItem>
                                    </Col>
                                </Row>
                                <Row type="flex" justify="space-around" class="code-row-bg">
                                    <Col span="7">
                                        <FormItem label="企业负责人：" prop="charge">
                                            {{ showdata.charge }}
                                        </FormItem>
                                    </Col>
                                    <Col span="7">
                                        <FormItem :label-width="200" label="二级集团安全环保管理人员：" prop="secondaryGroupPersion">
                                            {{ showdata.secondaryGroupPersion }}
                                        </FormItem>
                                    </Col>
                                    <Col span="7" v-if="showdata.injuryLevelId == '1000001019000000003'">
                                        <FormItem :label-width="180" label="集团安全环保管理人员：" prop="groupPersion">
                                            {{ showdata.groupPersion }}
                                        </FormItem>
                                    </Col>
                                    <Col v-else span="7"></Col>
                                </Row>
                                <!--<Row type="flex" justify="space-around" class="code-row-bg">-->
                                <!--<Col span="7">-->
                                <!--<FormItem label="填报单位：" prop="reportOrgName">-->
                                <!--{{ showdata.reportOrgName }}-->
                                <!--</FormItem>-->
                                <!--</Col>-->
                                <!--<Col span="7"></Col>-->
                                <!--</Row>-->
                                <Row>
                                    <Col span="23" class="query-form" style="margin-left: 15px">
                                        <Button v-show="mode !== 'view'" class="ivu-btn-add" icon="md-add"
                                                @click="addItem">新增伤亡人员
                                        </Button>
                                        <Table highlight-row stripe border style="margin-top:10px;margin-bottom: 10px"
                                               :max-height="350" :columns="itemColumn"
                                               :data="showdata.briefItems">
                                        </Table>
                                    </Col>
                                </Row>

                                <Row type="flex" justify="space-around" class="code-row-bg">
                                    <Col span="23">
                                        <FormItem label="初步原因分析：" prop="accAnalyze">
                                            <Input v-model="showdata.accAnalyze" type="textarea"
                                                   :autosize="{minRows:2,maxRows:4}"/>
                                        </FormItem>
                                    </Col>
                                </Row>
                                <Row type="flex" justify="space-around" class="code-row-bg">
                                    <Col span="23">
                                        <FormItem label="主要经过：" prop="accDesc">
                                            <Input v-model="showdata.accDesc" type="textarea"
                                                   :autosize="{minRows:2,maxRows:4}"/>
                                        </FormItem>
                                    </Col>
                                </Row>
                                <Row type="flex" justify="space-around" class="code-row-bg">
                                    <Col span="23">
                                        <FormItem label="备注：">
                                            <Input v-model="showdata.notes" type="textarea"
                                                   :autosize="{minRows:2,maxRows:4}"/>
                                        </FormItem>
                                    </Col>
                                </Row>

                                <Row type="flex" justify="space-around" class="code-row-bg">
                                    <Col span="23">
                                        <FormItem label="伤亡事故简报表：" prop="uploadList">
                                            <div>
                                                <Upload ref="upload"
                                                        :before-upload="beforeUpload"
                                                        :show-upload-list="true"
                                                        :max-size="10240"
                                                        multiple
                                                        type="drag"
                                                        :action="fileUrl">
                                                    <div style="padding: 10px">
                                                        <Icon type="ios-cloud-upload" size="45"
                                                              style="color: #3399ff"></Icon>
                                                        <p>点击将文件拖拽到这里上传</p>
                                                        <span style="font-size:8px;color: #797a64">支持扩展名：.rar .zip .doc .docx .pdf...</span>
                                                    </div>
                                                </Upload>
                                            </div>
                                            <div v-show="uploadList.length>0" v-for="item in uploadList" :key='item'>
                                                <a @click="downloadFile(form1datacopy.programmeId,item)">{{
                                                    item.fileName }}</a>
                                                <Button type="text" icon="md-trash" @click="remove1(item.id)"
                                                        style="margin-left: 5px;margin-bottom: 3px"/>
                                            </div>
                                        </FormItem>
                                    </Col>
                                </Row>

                            </Form>
                        </Col>
                    </div>
                    <div :style="{ overflowY: 'auto'}" v-else>
                        <Col span="24">
                            <Form :model="showdata" ref="showdata" :rules="ruleCustom" :label-width="160">
                                <Row type="flex" justify="space-around" class="code-row-bg">
                                    <Col span="15">
                                        <FormItem label="事故名称：" prop="victim">
                                            {{ showdata.sartName }}
                                        </FormItem>
                                    </Col>
                                    <Col span="7">
                                        <FormItem label="事故类别：" prop="accTypeName">
                                            {{ showdata.accTypeName }}
                                        </FormItem>
                                    </Col>
                                </Row>
                                <Row type="flex" justify="space-around" class="code-row-bg">
                                    <Col span="7">
                                        <FormItem label="直接经济损失(万元)：" prop="loseMoney">
                                            {{ showdata.loseMoney }}
                                        </FormItem>
                                    </Col>
                                    <Col span="7">
                                        <FormItem label="歇工天数：">
                                            {{ showdata.daysWork }}
                                        </FormItem>
                                    </Col>
                                    <Col span="7">
                                        <FormItem label="事故时间：" prop="position">
                                            {{ formatDate(new Date(showdata.sartDate), 'yyyy-MM-dd')}}
                                        </FormItem>
                                    </Col>
                                </Row>
                                <Row type="flex" justify="space-around" class="code-row-bg">
                                    <Col span="6">
                                        <FormItem label="企业负责人：" prop="charge">
                                            {{ showdata.charge }}
                                        </FormItem>
                                    </Col>
                                    <Col span="8">
                                        <FormItem :label-width="200" label="二级集团安全环保管理人员：" prop="secondaryGroupPersion">
                                            {{ showdata.secondaryGroupPersion }}
                                        </FormItem>
                                    </Col>
                                    <Col span="7" v-if="showdata.injuryLevelId == '1000001019000000003'">
                                        <FormItem :label-width="180" label="集团安全环保管理人员：" prop="groupPersion">
                                            {{ showdata.groupPersion }}
                                        </FormItem>
                                    </Col>
                                    <Col v-else span="7">
                                    </Col>
                                </Row>
                                <Row type="flex" justify="space-around" class="code-row-bg">
                                    <Col span="7">
                                        <FormItem label="填报人：" prop="userName">
                                            {{ showdata.userName }}
                                        </FormItem>
                                    </Col>
                                    <Col span="7">
                                        <FormItem label="填报人电话：" prop="phone">
                                            {{ showdata.phone }}
                                        </FormItem>
                                    </Col>
                                    <Col span="7">
                                        <FormItem label="填报人单位：" prop="reportOrgName">
                                            {{ showdata.reportOrgName }}
                                        </FormItem>
                                    </Col>
                                    <Col span="7"></Col>
                                </Row>

                                <Row>
                                    <Col span="23" class="query-form" style="margin-left: 15px">
                                        <Table highlight-row stripe border style="margin-top:10px;margin-bottom: 10px"
                                               :max-height="350" :columns="itemColumn"
                                               :data="showdata.briefItems">
                                        </Table>
                                    </Col>
                                </Row>

                                <Row type="flex" justify="space-around" class="code-row-bg">
                                    <Col span="23">
                                        <FormItem label="伤亡事故简报表：">
                                            <div v-show="uploadList.length>0" v-for="item in uploadList" :key='item'>
                                                <a @click="downloadFile(showdata.briefId,item)">{{ item.fileName }}</a>
                                            </div>
                                        </FormItem>
                                    </Col>
                                </Row>
                                <Row type="flex" justify="space-around" class="code-row-bg">
                                    <Col span="23">
                                        <FormItem label="初步原因分析：" prop="accAnalyze">
                                            {{ showdata.accAnalyze }}
                                        </FormItem>
                                    </Col>

                                </Row>
                                <Row type="flex" justify="space-around" class="code-row-bg">
                                    <Col span="23">
                                        <FormItem label="主要经过：" prop="accDesc">
                                            {{ showdata.accDesc }}
                                        </FormItem>
                                    </Col>

                                </Row>
                                <Row type="flex" justify="space-around" class="code-row-bg">
                                    <Col span="23">
                                        <FormItem label="备注：">
                                            {{ showdata.notes }}
                                        </FormItem>
                                    </Col>

                                </Row>

                                <Row v-if="!taskInfo.viewFlag">
                                    <Col span="11">
                                        <FormItem label="审批结果：" prop="route">
                                            <Select v-model="showdata.route" @on-change="routePush">
                                                <Option v-for="item in approveList" :value="item.value"
                                                        :key="item.value">{{
                                                    item.text
                                                    }}
                                                </Option>
                                            </Select>
                                        </FormItem>
                                    </Col>

                                </Row>
                                <Row v-if="!taskInfo.viewFlag">
                                    <Col span="11">
                                        <FormItem label="处理意见：" prop="opinion">
                                            <Input v-model="showdata.opinion" type="textarea" :rows="2"/>
                                        </FormItem>
                                    </Col>
                                </Row>

                            </Form>
                        </Col>
                    </div>
                </Card>
            </div>
        </div>

        <Modal :mask-closable="false" :transfer=false v-model="itemModel" :width=600 title="伤亡人员"
               @on-cancel="itemModel=false,briefItem={}">
            <Row>
                <Col span="24">
                    <Form ref="itemForm" :class="'modal-form'" :show-message="false" :model="briefItem"
                          :rules="itemRuleCustom" label-position="top">
                        <Row type="flex" justify="space-around" class="code-row-bg">
                            <Col span="11">
                                <FormItem label='伤亡人员姓名：' prop='victim'>
                                    <Input class='query-form-el' placeholder="请填写伤亡人员姓名" type='text'
                                           v-model='briefItem.victim'></Input>
                                </FormItem>
                            </Col>
                            <Col span="11">
                                <FormItem label='性别：' prop='gender'>
                                    <!--<Input class='query-form-el' placeholder="请填写性别" type='text' v-model='dataToView.gender'></Input>-->
                                    <Select v-model="briefItem.gender" clearable>
                                        <Option v-for="item in sexList" :value="item.value" :key="item.value">{{
                                            item.text }}
                                        </Option>
                                    </Select>
                                </FormItem>
                            </Col>
                        </Row>
                        <Row type="flex" justify="space-around" class="code-row-bg">
                            <Col span="11">
                                <FormItem label='年龄：' prop='age'>
                                    <InputNumber class='query-form-el' :min="0" placeholder="请填写年龄"
                                                 v-model='briefItem.age'></InputNumber>
                                </FormItem>
                            </Col>
                            <Col span="11">
                                <FormItem label='岗位或工种：' prop='position'>
                                    <Input class='query-form-el' placeholder="请填写岗位或工种" type='text'
                                           v-model='briefItem.position'></Input>
                                </FormItem>
                            </Col>
                        </Row>
                        <Row type="flex" justify="space-around" class="code-row-bg">
                            <Col span="11">
                                <FormItem label='伤害部位：' prop='injured'>
                                    <Input class='query-form-el' placeholder="请填写伤害部位" type='text'
                                           v-model='briefItem.injured'></Input>
                                </FormItem>
                            </Col>
                            <Col span="11">
                                <FormItem label='就业类型：' prop='membershipId'>
                                    <!--<Input class='query-form-el' placeholder="请填写就业类型" type='text' v-model='dataToView.membershipId'></Input>-->
                                    <Select v-model="briefItem.membershipId" @on-change="selectMembership"
                                            :label-in-value="true">
                                        <Option v-for="item in membershipList" :value="item.codingCodeId"
                                                :key="item.codingCodeId">
                                            {{ item.codingName }}
                                        </Option>
                                    </Select>
                                </FormItem>
                            </Col>
                        </Row>
                        <Row type="flex" justify="space-around" class="code-row-bg">
                            <Col span="11">
                                <FormItem label='伤害程度：' prop='injuryLevelId'>
                                    <!--<Input class='query-form-el' placeholder="请填写伤害程度" type='text' v-model='dataToView.injuryLevelId'></Input>-->
                                    <Select v-model="briefItem.injuryLevelId" @on-change="selectInJuryLevelId"
                                            :label-in-value="true">
                                        <Option v-for="item in injuryLeveList" :value="item.codingCodeId"
                                                :key="item.codingCodeId">
                                            {{ item.codingName }}
                                        </Option>
                                    </Select>
                                </FormItem>
                            </Col>
                            <Col span="11">
                                <FormItem label='歇工天数：' prop='daysWork'>
                                    <InputNumber class='query-form-el' placeholder="请填写歇工天数"
                                                 :disabled="daysWork" :min="0"
                                                 v-model='briefItem.daysWork'></InputNumber>
                                </FormItem>
                            </Col>
                        </Row>
                        <Row type="flex" justify="space-around" class="code-row-bg">
                            <Col span="11">
                                <FormItem label='直接经济损失(万元)：' prop='loseMoney'>
                                    <InputNumber class='query-form-el' placeholder="请填写0-9数字包括小数" :min="0"
                                                 v-model='briefItem.loseMoney'></InputNumber>
                                </FormItem>
                            </Col>
                            <Col span="11"></Col>
                        </Row>
                    </Form>
                </Col>

            </Row>
            <div slot="footer">
                <Button type="primary" class='ivu-btn-save' @click="itemSubmit('itemForm')">确定</Button>
                <Button type="primary" class='ivu-btn-cancel' @click="itemModel=false,briefItem={}">取消</Button>
                <!--<Button v-if="mode === 'view'" class='ivu-btn-save' @click="dialogShow=false">关闭</Button>-->
            </div>
        </Modal>

    </Layout>
</template>


<script>
    import {Api} from '../../../../api/index.js'
    import {sessionStorage, cookie} from '../../../../common/cacheUtils.js'
    import cascaderDom from "../../../../components/cascader-dom.vue"

    export default {
        components: {
            cascaderDom
        },
        data() {
            return {
                taskInfo: {},
                user: {},
                buttonloading1: false,
                fileUrl: Api.interestedPartiesUserFile,

                evaluationPeriodDate: [null, null],
                contractTermDate: [null, null],
                uploadList: [],
                evaluationPeriodDate1: null,
                contractTermDate1: null,
                viewFlag: false,
                flag: false,
                daysWork: false,
                approveFlag: '',
                approveList: [
                    {
                        text: "通过",
                        value: "1"
                    },
                    {
                        text: "不通过",
                        value: "2"
                    }
                ],
                showdata: {},
                tableHeight: 450,
                file: null,
                fileName: null,
                downloadUrl: Api.downloadMongoFile,
                formList: [],

                ruleCustom1: {
                    phone: [{
                        validator: (rule, value, callback) => {
                            if (!(/^1[3456789]\d{9}$/.test(value))) {
                                callback(' ');
                            } else {
                                callback()
                            }
                        },
                        required: true,
                        trigger: 'change'
                    }],

                    victim: [{
                        message: '  ',
                        required: true,
                        trigger: 'blue'
                    }],
                    uploadList: [{
                        validator: (rule, value, callback) => {
                            if (this.uploadList == 0) {
                                callback(' ')
                            } else {
                                callback()
                            }
                        },
                        required: true,
                        message: '请上传附件',
                        trigger: 'change'
                    }],
                    /*         daysWork: [ { required: true, message: '请填写歇工天数', trigger: 'change',type:'number'}],*/
                    daysWork: [{
                        validator: (rule, value, callback) => {
                            if (this.showdata.daysWork == undefined || this.showdata.daysWork == null || this.showdata.daysWork == '') {
                                callback(' ')
                            } else {
                                callback()
                            }
                        },
                        message: '请填写歇工天数',
                        required: true,
                        trigger: 'change'
                    }],
                    loseMoney: [{
                        required: true, trigger: 'change', message: ' ',
                        validator: (rule, value, callback) => {
                            if (!(/^(([^0][0-9]+|0)\.([0-9]{1,8})$)|^(([^0][0-9]+|0)$)|^(([1-9]+)\.([0-9]{1,8})$)|^(([1-9]+)$)/.test(value))) {
                                callback(' ');
                            } else {
                                callback()
                            }
                        },
                    }],
                    groupPersionId: [{
                        validator: (rule, value, callback) => {
                            if (this.showdata.groupPersionId == null || this.showdata.groupPersionId == '' || this.showdata.groupPersionId == undefined) {
                                callback(' ')
                            } else {
                                callback()
                            }
                        },
                        required: true,
                        trigger: 'change'
                    }],
                    chargeId: [{
                        validator: (rule, value, callback) => {
                            if (this.showdata.chargeId == null || this.showdata.chargeId == '' || this.showdata.chargeId == undefined) {
                                callback(' ')
                            } else {
                                callback()
                            }
                        },
                        required: true,
                        trigger: 'change'
                    }],
                    secondaryGroupPersionId: [{
                        validator: (rule, value, callback) => {
                            if (this.showdata.secondaryGroupPersionId == null || this.showdata.secondaryGroupPersionId == '' || this.showdata.secondaryGroupPersionId == undefined) {
                                callback(' ')
                            } else {
                                callback()
                            }
                        },
                        required: true,
                        trigger: 'change'
                    }],
                    sartDate: [{
                        validator: (rule, value, callback) => {
                            if (value == null || value == '' || value == undefined) {
                                callback(' ')
                            } else {
                                callback()
                            }
                        },
                        required: true,
                        trigger: 'change'
                    }],
                    accTypeId: [{
                        message: '  ',
                        required: true,
                        trigger: 'change'
                    }],
                    contacterPhone: [{
                        validator: (rule, value, callback) => {
                            if (!(/^1[3456789]\d{9}$/.test(value))) {
                                callback(' ');
                                this.$Message.error("请填写有效的的手机号")
                            } else {
                                callback()
                            }
                        },
                        required: true,
                        trigger: 'change'
                    }],

                },
                ruleCustom: {
                    opinion: [{
                        validator: (rule, value, callback) => {
                            if (this.showdata.opinion == null || this.showdata.opinion == '' || this.showdata.opinion == undefined) {
                                callback(' ')
                            } else {
                                callback()
                            }
                        },
                        required: true,
                        trigger: 'change, blur'
                    }],
                    route: [
                        {required: true, message: '  ', trigger: 'blur'}
                    ]
                },
                typeList: [],
                membershipList: [],
                sexList: [
                    {
                        value: '0',
                        text: '男'
                    },
                    {
                        value: '1',
                        text: '女'
                    }
                ],

                briefItem: {},
                itemModel: false,
                itemAdd: false,
                columnsItem: [
                    // {title: '序号', width: 70, align: 'center', render: this.indexColumnsRender},
                    {title: '伤亡人员姓名', align: 'center', key: 'victim', minWidth: 100,},
                    {title: '性别', align: 'center', key: 'gender', minWidth: 80, render: (h, params) => {
                            return h('span', params.row.gender == '0' ? '男' : '女')}},
                    {title: '年龄', align: 'center', key: 'age', minWidth: 80,},
                    {title: '工种或岗位', align: 'center', key: 'position', minWidth: 120,},
                    {title: '伤害部位', align: 'center', key: 'injured', minWidth: 120,},
                    {title: '就业类型', align: 'center', key: 'membershipName', minWidth: 120,},
                    {title: '伤害程度', align: 'center', key: 'injuryLevelName', minWidth: 120,},
                    {title: '歇工天数', align: 'center', key: 'daysWork', minWidth: 100,},
                    {title: '直接经济损失(万元)', align: 'center', key: 'loseMoney', minWidth: 120,},
                    {
                        title: '操作',
                        key: 'action',
                        fixed: 'right',
                        width: 110,
                        align: 'center',
                        render: (h, params) => {
                            return h('div', [
                                h('A', {
                                    props: {type: 'primary', size: 'small'},
                                    style: {fontSize: '13px',},
                                    on: {
                                        click: () => {
                                            this.briefItem = params.row
                                            this.itemModel = true
                                            this.itemAdd = false
                                        }
                                    }
                                }, '修改'),
                                h('Divider', {props: {type: 'vertical'}}, ''),
                                h('A', {
                                    props: {type: 'primary', size: 'small'},
                                    style: {fontSize: '13px', color: 'red'},
                                    on: {
                                        click: () => {
                                            this.$Modal.confirm({
                                                title: '提示',
                                                content: '<p>是否要删除？</p>',
                                                onOk: () => {
                                                    this.showdata.briefItems.splice(params.index, 1)
                                                    this.showdata.loseMoney = 0
                                                    this.showdata.daysWork = 0
                                                    this.showdata.injuryLevelId = '0'
                                                    let victim = []
                                                    this.showdata.briefItems.forEach(m => {
                                                        this.showdata.loseMoney += m.loseMoney
                                                        this.showdata.daysWork += m.daysWork
                                                        victim.push(m.victim)
                                                        if (m.injuryLevelId > this.showdata.injuryLevelId) {
                                                            this.showdata.injuryLevelId = m.injuryLevelId
                                                        }
                                                    })
                                                    this.showdata.victim = victim.join(',')
                                                    this.itemModel = false
                                                }
                                            })
                                        }
                                    }
                                }, '删除'),
                            ])
                        }
                    }
                ],
                itemRuleCustom: {
                    victim: [{required: true, message: '请填写伤亡人员姓名', trigger: 'change'}],
                    gender: [{required: true, message: '请选择性别', trigger: 'change'}],
                    age: [{required: true, message: '请填写年龄', trigger: 'change', pattern: /^[0-9]*$/,}],
                    position: [{required: true, message: '请填写工种或岗位', trigger: 'change'}],
                    injured: [{required: true, message: '请填写伤害部位', trigger: 'change'}],
                    membershipId: [{required: true, message: '请选择就业类型', trigger: 'change'}],
                    injuryLevelId: [{required: true, message: '请选择伤害程度', trigger: 'change'}],
                    daysWork: [{required: true, message: '请填写歇工天数', trigger: 'change', type: 'number'}],
                    loseMoney: [{required: true, message: '请填写直接经济损失', trigger: 'change', type: 'number'}],
                },
            }
        },
        computed: {
            itemColumn() {
                const columns = [...this.columnsItem];
                const columns1 = []
                columns.forEach(item => {
                    if (this.taskInfo.procinstState === '2') {
                        columns1.push(item)
                    } else {
                        if (item.key !== 'action') {
                            columns1.push(item)
                        }
                    }
                })
                return columns1
            },
        },
        mounted: function () {
            this.tableHeight = window.innerHeight - 190;
            this.selectCodingSort();
            if (this.$route.query.close == false) {
                this.$http.get(Api.getUserByUserId + this.$route.query.userId)
                    .then(function (res) {
                        this.user = res.data.data;
                        this.$http.get(Api.getTaskData + "/" + this.$route.query.procinstId + "/" +
                            this.$route.query.taskId + "?close=" + this.$route.query.close).then(function (res1) {
                            this.taskInfo = res1.data;
                            if (res1.data.viewFlag == false)
                                this.taskInfo.viewFlag = false;
                            else
                                this.taskInfo.viewFlag = true;
                            this.query();
                            return;
                        })
                    })
            } else {
                var search = window.location.href.split('?')[1];//截取URL？后面的参数
                var params = {};
                if (search != "" && search != undefined) {
                    search.split("&").forEach(
                        function (val) {
                            var arr = val.split("=");
                            params[arr[0]] = arr[1];
                        }
                    )
                }
                var code = params.code
                var status = params.status
                if (code != null && code != 'undefine') {
                    this.$http.get(Api.queryToken, {
                        params: {
                            code: code,
                        }
                    }).then(res1 => {
                        if (res1.data.errcode == "1014") {
                            this.$http.get(Api.getReZheng).then(res2 => {
                                window.location.href = decodeURIComponent(res2.bodyText) + "&redirect_uri=" + encodeURIComponent(window.location.href);
                            })
                        } else {
                            this.$http.get(Api.getUserByUserId + this.$route.query.userId)
                                .then(function (res) {
                                    this.user = res.data.data;
                                    this.$http.get(Api.getTaskData + "/" + this.$route.query.procinstId + "/" +
                                        this.$route.query.taskId + "?close=" + this.$route.query.close).then(function (res1) {
                                        this.taskInfo = res1.data;
                                        if (res1.data.viewFlag == false)
                                            this.taskInfo.viewFlag = false;
                                        else
                                            this.taskInfo.viewFlag = true;
                                        this.query();
                                        return;
                                    })
                                })
                        }
                    })
                } else {
                    this.$http.get(Api.getReZheng).then(res2 => {
                        window.location.href = decodeURIComponent(res2.bodyText) + "&redirect_uri=" + encodeURIComponent(window.location.href);
                    });
                }
            }
        },
        methods: {

            selectInJuryLevelId(value) {
                if (value) {
                    this.briefItem.injuryLevelId = value.value;
                    this.briefItem.injuryLevelName = value.label;
                    if (this.briefItem.membershipId === 'MEM-000001000000004' && value.value === '1000001019000000003') {
                        this.briefItem.daysWork = 0;
                    } else {
                        if (value.value === '1000001019000000003') {
                            this.briefItem.daysWork = 6000;
                        }
                    }
                }
            },
            selectMembership(value) {
                if (value) {
                    this.briefItem.membershipId = value.value;
                    this.briefItem.membershipName = value.label;
                    if (value.value === 'MEM-000001000000004') {
                        this.daysWork = true;
                        this.briefItem.daysWork = 0;
                    } else {
                        this.daysWork = false;
                        if (this.briefItem.injuryLevelId === '1000001019000000003') {
                            this.briefItem.daysWork = 6000;
                        }
                    }
                }
            },
            addItem() {
                this.itemModel = true
                this.itemAdd = true;
                this.briefItem = {loseMoney: 0, daysWork: 0, age: 1}
            },
            itemSubmit(name) {
                this.$refs[name].validate((valid) => {
                    if (valid) {
                        this.$Modal.confirm({
                            title: '提示',
                            content: '<p>是否要提交？</p>',
                            onOk: () => {
                                let _this = this
                                _this.showdata.briefItems.splice()
                                if (_this.itemAdd) {
                                    _this.showdata.briefItems.push(_this.briefItem)
                                } else {
                                    _this.showdata.briefItems[_this.briefItem._index] = _this.briefItem
                                }
                                _this.showdata.loseMoney = 0
                                _this.showdata.daysWork = 0
                                let victim = []
                                _this.showdata.injuryLevelId='0'
                                _this.showdata.briefItems.forEach(m => {
                                    _this.showdata.loseMoney += m.loseMoney
                                    _this.showdata.daysWork += m.daysWork
                                    victim.push(m.victim)
                                    if (m.injuryLevelId > _this.showdata.injuryLevelId) {
                                        _this.showdata.injuryLevelId = m.injuryLevelId
                                    }
                                })
                                _this.showdata.victim = victim.join(',')
                                _this.itemModel = false
                            },
                            onCancel: () => {
                            }
                        })
                    } else {
                        this.$Message.error('请填报完整!');
                    }
                })
            },


            returnTo() {
                if (this.taskInfo.close == true)
                    window.close();
                else
                    this.$router.go(-1);
            },
            routePush(value) {
                if (value === '1') {
                    this.showdata.opinion = "同意";
                } else {
                    this.showdata.opinion = null;
                }
            },
            selectCodingSort() {
                this.$http.get(Api.queryCodeDetailBySortId + '/MEM-000001', {params: {}}).then((res) => {
                    this.membershipList = res.data.data;
                });
                this.$http.get(Api.queryCodeDetailBySortId + '/ACC-000001', {params: {}}).then((res) => {
                    this.typeList = res.data.data;
                });
                this.$http.get(Api.queryCodeDetailBySortId + '/1000001019', {params: {}}).then((res) => {
                    this.injuryLeveList = res.data.data;
                });


            },
            remove1(id) {
                this.$Modal.confirm({
                    title: '提示',
                    content: '<p>是否要删除该文件？</p>',
                    onOk: () => {
                        let url = this.fileUrl + '/file/' + id;
                        this.$http.delete(url).then((res) => {
                            this.getFiles()
                        })
                    }
                })
            },

            beforeUpload(file) {
                let formData = new FormData();
                formData.append("files", file); // 文件对象
                let url = this.fileUrl + '/' + this.showdata.briefId + '/files/guideFile'
                this.$http.post(url, formData).then((res) => {
                    if (res.data.result == "success") {
                        this.$Message.info("文件上传成功")
                        this.getFiles();
                    }
                })
                return false
            },
            getFiles() {
                let url = this.fileUrl + '/' + this.showdata.briefId + '/files/guideFile'
                this.$http.get(url).then((res) => {
                    if (res.data.result == "success") {
                        this.uploadList = res.data.data
                        console.log(this.uploadList);
                    }
                })
            },

            downloadFile(perfId, file) {

                let url = Api.interestedPartiesUserFile + '/file/' + file.id;
                this.$http.get(url, {
                    responseType: 'blob'      //告诉服务器我们需要的响应格式
                }).then((res) => {
                    console.log(res)
                    const aLink = document.createElement("a");
                    let blob = new Blob([res.data], {
                        type: res.bodyBlob.type      //将会被放入到blob中的数组内容的MIME类型
                    });
                    aLink.href = URL.createObjectURL(blob)
                    aLink.download = file.fileName //标题
                    aLink.click()
                    document.body.appendChild(aLink)
                })
            },

            query() {
                let url = Api.getBriefing;
                let id = this.taskInfo.workId;

                this.$http.get(url + id, {params: {}}).then((res) => {
                    this.showdata = res.data.data;
                    if (this.showdata.membershipId === 'MEM-000001000000004') {
                        this.daysWork = true;
                    } else {
                        this.daysWork = false;
                    }

                    let url = Api.interestedPartiesUserFile + '/' + id + '/files/guideFile';
                    this.$http.get(url).then((res1) => {
                        this.uploadList = res1.data.data
                    }).catch(e => {
                        this.$Message.error("数据获取失败")
                    });


                }).catch(e => {
                    this.$Message.error("数据获取失败")
                })
            },

            submitAndUpdate() {
                if (this.showdata.briefItems.length < 1) {
                    this.$Message.warning('伤亡人员至少填报一条');
                } else {
                    this.$refs['showdata'].validate((valid) => {
                        if (valid) {
                            this.buttonloading1 = true;
                            let data = {};
                            data = {
                                createUserId: this.user.userId,
                                userName: this.user.userName,
                                opinion: this.showdata.opinion,
                                taskId: this.taskInfo.taskId,
                                procinstId: this.taskInfo.procinstId,
                                key: "reportList"
                            };

                            let url = Api.briefing;
                            this.showdata.updateUserId = this.user.userId
                            this.showdata.updateOrgId = this.user.orgInfo.orgId
                            let sartDate = this.formatDate(new Date(this.showdata.sartDate), "yyyy-MM-dd")
                            this.showdata.sartName = sartDate + '伤亡事故(' + this.showdata.victim + ')'

                            this.$http.put(url, this.showdata).then((res) => {
                                if (res.data.status == '200') {
                                    data.route = '1';
                                    this.$http.get(Api.taskComplete, {params: data}).then((res) => {
                                        this.buttonloading1 = false;
                                        if (res.data.status == '200') {
                                            this.$Message.success("审批成功")
                                            this.returnTo();
                                        } else {
                                            this.$Message.success("审批失败")
                                        }
                                    }).catch(e => {
                                        this.$Message.error("数据获取失败")
                                    })
                                } else {
                                    this.buttonloading1 = false;
                                }
                            }).catch(e => {
                                this.$Message.error('请求失败');
                                this.buttonloading1 = false;

                            })


                        }
                    })
                }
            },
            submit() {
                this.$refs['showdata'].validate((valid) => {
                    if (valid) {
                        this.buttonloading1 = true;
                        let data = {};
                        data = {
                            createUserId: this.user.userId,
                            userName: this.user.userName,
                            opinion: this.showdata.opinion,
                            taskId: this.taskInfo.taskId,
                            procinstId: this.taskInfo.procinstId,
                            route: this.showdata.route,
                            key: "reportList"
                        };
                        if (this.showdata.route != '2') {
                            if (this.showdata.route == '1' && this.showdata.injuryLevelId == '1000001019000000003') {
                                data.route = '1';
                                // data.user3 = this.showdata.groupPersion
                            } else {
                                data.route = '5';
                                // data.user3 = ''
                            }
                        }
                        this.$http.get(Api.taskComplete, {params: data}).then((res) => {
                            this.buttonloading1 = false;
                            if (res.data.status == '200') {
                                this.$Message.success("审批成功")
                                this.returnTo();
                            } else {
                                this.$Message.success("审批失败")
                            }
                        }).catch(e => {
                            this.buttonloading1 = false;
                            this.$Message.error("数据获取失败")
                        })


                    } else {
                        this.$Message.error('请填报完整!');
                    }
                })
            },


            formatDate(date, fmt) {
                if (/(y+)/.test(fmt)) {
                    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
                }
                let o = {
                    'M+': date.getMonth() + 1,
                    'd+': date.getDate(),
                    'h+': date.getHours(),
                    'm+': date.getMinutes(),
                    's+': date.getSeconds()
                };
                for (let k in o) {
                    if (new RegExp(`(${k})`).test(fmt)) {
                        let str = o[k] + '';
                        fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? str : this.padLeftZero(str));
                    }
                }
                return fmt;
            },
            padLeftZero(str) {
                return ('00' + str).substr(str.length);
            },
        },
    }
</script>
<style lang='css' scoped>
    @import '../../../../index.css';

</style>
